I am trying to apply a coupon while I am creating a payment Intent in stripe. Every thing is going well but I cant manually update the number of times that coupon had been used.
I have checked the stripe doc, but found nothing. Can Someone help?
This is the stripe coupon specific doc: Stripe Coupon Object API Doc
This is my code where I am changing the price according to the percent_off of the coupon. Everything goes well, but at the end when I am calling updateCoupon function its showing me error.
import { stripe } from "../utils/stripeInstance";
export const createPaymentIntent = async (req, res) => {
try {
const { tplan, couponCode, cust_id } = req.body;
console.log("Coupon Code==>", couponCode);
let price = 0;
//coupon redeemed
let cr = 0;
if (tplan === "smash") price = 599 * 100;
else if (tplan === "smash+") price = 999 * 100;
else return res.status(400).send("Invalid plan");
console.log("Price Before Coupon==>", price);
if (couponCode) {
const coupon = await stripe.coupons.retrieve(couponCode);
if (coupon.valid) {
price = price - (price * coupon.percent_off) / 100;
cr = coupon.times_redeemed;
}
}
console.log("Price After Coupon==>", price);
const paymentIntent = await stripe.paymentIntents.create({
amount: price,
currency: "inr",
payment_method_types: ["card"],
customer: cust_id,
});
const updateCoupon = await stripe.coupons.update(couponCode, {
times_redeemed: cr + 1,
metadata: {
used: true,
},
});
return res.status(200).send({
paymentId: paymentIntent.id,
status: paymentIntent.status,
clientSecret: paymentIntent.client_secret,
});
} catch (err) {
res.status(500).json({ statusCode: 500, message: err.message });
}
};
Error is only when I am updating the coupon by calling updateCoupon function above.This is showing in error.
Basically what I am assuming that it does not getting proper attributes, may be there is any other correct way to do it. Can anyone help me , Thank You ❤️
The only things about a coupon you can update once it is created are the metadata and the name properties, as specified here.
The times_redeemed is incremented by Stripe when a coupon is applied to a customer's purchase as part of an invoice.
However, it appears there is a larger confusion here. The reason you are having to write all these functions (fetching the coupon, modifying the amount to charge, etc.) is you are trying to apply coupons to one-off charges with Payment Intents.
Also why Stripe does not allow manual updating of times_redeemed. It's intended as an internal counter and only incremented as it processes a charge where the coupon code was applied.
Coupons do not work with conventional one-off charges.
You can review how Stripe Coupons are supposed to function with Subscriptions here.
If you want to offer your customers a way to apply your own coupon discounts to the amount you charge, you will need to maintain those records in your own data models. You could use the Stripe Coupon object as a starting point and design your own database records.
But at present Stripe does not support coupons for one-off purchases